<span id="Label3"> <p>第一步：首先确定一下，你所使用的方式，是通过外部邮箱（比如QQ还是163）还是内部邮箱</p> <p>先新建一个model</p>
 <div class="code">
  <pre><span style="color: #0000ff;">using</span><span style="color: #000000;"> System;
</span><span style="color: #0000ff;">using</span><span style="color: #000000;"> System.Collections.Generic;
</span><span style="color: #0000ff;">using</span><span style="color: #000000;"> System.Text;

</span><span style="color: #0000ff;">namespace</span><span style="color: #000000;"> WebApi.Core.Model
{
    </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> MailModel
    {
        </span><span style="color: #808080;">///</span> <span style="color: #808080;">&lt;summary&gt;</span>
        <span style="color: #808080;">///</span><span style="color: #008000;"> 发送人
        </span><span style="color: #808080;">///</span> <span style="color: #808080;">&lt;/summary&gt;</span>
        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> fromPerson { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span><span style="color: #000000;">; }

        </span><span style="color: #808080;">///</span> <span style="color: #808080;">&lt;summary&gt;</span>
        <span style="color: #808080;">///</span><span style="color: #008000;"> 收件人地址(多人)
        </span><span style="color: #808080;">///</span> <span style="color: #808080;">&lt;/summary&gt;</span>
        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span>[] recipientArry { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span><span style="color: #000000;">; }

        </span><span style="color: #808080;">///</span> <span style="color: #808080;">&lt;summary&gt;</span>
        <span style="color: #808080;">///</span><span style="color: #008000;"> 抄送地址(多人)
        </span><span style="color: #808080;">///</span> <span style="color: #808080;">&lt;/summary&gt;</span>
        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span>[] mailCcArray { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span><span style="color: #000000;">; }

        </span><span style="color: #808080;">///</span> <span style="color: #808080;">&lt;summary&gt;</span>
        <span style="color: #808080;">///</span><span style="color: #008000;"> 标题
        </span><span style="color: #808080;">///</span> <span style="color: #808080;">&lt;/summary&gt;</span>
        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> mailTitle { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span><span style="color: #000000;">; }

        </span><span style="color: #808080;">///</span> <span style="color: #808080;">&lt;summary&gt;</span>
        <span style="color: #808080;">///</span><span style="color: #008000;"> 正文
        </span><span style="color: #808080;">///</span> <span style="color: #808080;">&lt;/summary&gt;</span>
        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> mailBody { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span><span style="color: #000000;">; }

        </span><span style="color: #808080;">///</span> <span style="color: #808080;">&lt;summary&gt;</span>
        <span style="color: #808080;">///</span><span style="color: #008000;"> 客户端授权码(可存在配置文件中)
        </span><span style="color: #808080;">///</span> <span style="color: #808080;">&lt;/summary&gt;</span>
        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> code { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span><span style="color: #000000;">; }

        </span><span style="color: #808080;">///</span> <span style="color: #808080;">&lt;summary&gt;</span>
        <span style="color: #808080;">///</span><span style="color: #008000;"> SMTP邮件服务器
        </span><span style="color: #808080;">///</span> <span style="color: #808080;">&lt;/summary&gt;</span>
        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">string</span> host { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span><span style="color: #000000;">; }

        </span><span style="color: #808080;">///</span> <span style="color: #808080;">&lt;summary&gt;</span>
        <span style="color: #808080;">///</span><span style="color: #008000;"> 正文是否是html格式
        </span><span style="color: #808080;">///</span> <span style="color: #808080;">&lt;/summary&gt;</span>
        <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">bool</span> isbodyHtml { <span style="color: #0000ff;">get</span>; <span style="color: #0000ff;">set</span><span style="color: #000000;">; }
    }
}</span></pre>
 </div> <p>然后在新建一个helper类，这里面做了一个重载，传model那个是为了在接口测试方便，主要是使用第二个方法</p> <p>里面的Appsettings是读取配置文件信息，如果没有可以在方法里面直接写，这里面的code 就是区分是外部还是内部了</p> <p>如果你是外部的，就百度一下 比如163怎么获取授权码，很好找的，然后给mails.code 赋值就可以了，如果是外部的host查一下你所使用的地址（目前只写了两个常用的）</p> <p>如果是局域网（我使用的就是内部的邮箱）就把你要当做发件人的 密码 赋值给 mails.code 并且 Host 配置 是你邮箱服务器的IP地址，可以用IP也可以用域名</p>
 <div class="code">
  <pre><span style="color: #000000;">using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using WebApi.Core.Model;

namespace WebApi.Core.Common
{
    </span>/// &lt;summary&gt;
    ///<span style="color: #000000;"> 发送邮件帮助类
    </span>///<span style="color: #000000;"> 创建人：金永宝
    </span>///<span style="color: #000000;"> 创建时间：2020年8月11日
    </span>/// &lt;/summary&gt;<span style="color: #000000;">
    public static class SendMailHelper
    {
        </span>/// &lt;summary&gt;
        ///<span style="color: #000000;"> 根据实体类发送邮件
        </span>///<span style="color: #000000;"> 创建人：金永宝
        </span>///<span style="color: #000000;"> 创建时间：2020年8月11日
        </span>/// &lt;/summary&gt;
        /// &lt;<span style="color: #0000ff;">param</span> name=<span style="color: #800000;">"</span><span style="color: #800000;">mails</span><span style="color: #800000;">"</span>&gt;&lt;/<span style="color: #0000ff;">param</span>&gt;
        /// &lt;returns&gt;&lt;/returns&gt;<span style="color: #000000;">
        public static void SendMail(MailModel mails)
        {
            string[] sArray </span>= mails.fromPerson.Split(new char[2] { ‘@<span style="color: #000000;">‘, ‘.‘ });
            </span><span style="color: #0000ff;">if</span> (sArray[1] == <span style="color: #800000;">"</span><span style="color: #800000;">qq</span><span style="color: #800000;">"</span><span style="color: #000000;">)
            {
                mails.host </span>= <span style="color: #800000;">"</span><span style="color: #800000;">smtp.qq.com</span><span style="color: #800000;">"</span>;//如果是QQ邮箱则：smtp.qq.com,依次类推  163:smtp.163<span style="color: #000000;">.com
            }
            </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> (sArray[1] == <span style="color: #800000;">"</span><span style="color: #800000;">163</span><span style="color: #800000;">"</span><span style="color: #000000;">)
            {
                mails.host </span>= <span style="color: #800000;">"</span><span style="color: #800000;">smtp.163.com</span><span style="color: #800000;">"</span>;//如果是QQ邮箱则：smtp.qq.com,依次类推  163:smtp.163<span style="color: #000000;">.com
            }

            </span>//<span style="color: #000000;">将发件人邮箱带入MailAddress中初始化
            MailAddress mailAddress </span>=<span style="color: #000000;"> new MailAddress(mails.fromPerson);
            </span>//<span style="color: #000000;">创建Email的Message对象
            MailMessage mailMessage </span>=<span style="color: #000000;"> new MailMessage();

            </span>//<span style="color: #000000;">判断收件人数组中是否有数据
            </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (mails.recipientArry.Any())
            {
                </span>//<span style="color: #000000;">循环添加收件人地址
                </span><span style="color: #0000ff;">foreach</span> (var item <span style="color: #0000ff;">in</span><span style="color: #000000;"> mails.recipientArry)
                {
                    </span><span style="color: #0000ff;">if</span> (!<span style="color: #000000;">string.IsNullOrEmpty(item))
                        mailMessage.To.Add(item.ToString());
                }
            }

            </span>//<span style="color: #000000;">判断抄送地址数组是否有数据
            </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (mails.mailCcArray.Any())
            {
                </span>//<span style="color: #000000;">循环添加抄送地址
                </span><span style="color: #0000ff;">foreach</span> (var item <span style="color: #0000ff;">in</span><span style="color: #000000;"> mails.mailCcArray)
                {
                    </span><span style="color: #0000ff;">if</span> (!<span style="color: #000000;">string.IsNullOrEmpty(item))
                        mailMessage.To.Add(item.ToString());
                }
            }
            </span>//<span style="color: #000000;">发件人邮箱
            mailMessage.From </span>=<span style="color: #000000;"> mailAddress;
            </span>//<span style="color: #000000;">标题
            mailMessage.Subject </span>=<span style="color: #000000;"> mails.mailTitle;
            </span>//<span style="color: #000000;">编码
            mailMessage.SubjectEncoding </span>=<span style="color: #000000;"> Encoding.UTF8;
            </span>//<span style="color: #000000;">正文
            mailMessage.Body </span>=<span style="color: #000000;"> mails.mailBody;
            </span>//<span style="color: #000000;">正文编码
            mailMessage.BodyEncoding </span>= Encoding.<span style="color: #0000ff;">Default</span><span style="color: #000000;">;
            </span>//<span style="color: #000000;">邮件优先级
            mailMessage.Priority </span>=<span style="color: #000000;"> MailPriority.High;
            </span>//<span style="color: #000000;">正文是否是html格式
            mailMessage.IsBodyHtml </span>=<span style="color: #000000;"> mails.isbodyHtml;
            </span>//<span style="color: #000000;">取得Web根目录和内容根目录的物理路径
            string webRootPath </span>=<span style="color: #000000;"> string.Empty;

            </span>//<span style="color: #000000;">实例化一个Smtp客户端
            SmtpClient smtp </span>=<span style="color: #000000;"> new SmtpClient();
            </span>//<span style="color: #000000;">将发件人的邮件地址和客户端授权码带入以验证发件人身份
            smtp.Credentials </span>=<span style="color: #000000;"> new System.Net.NetworkCredential(mails.fromPerson, mails.code);
            </span>//<span style="color: #000000;">指定SMTP邮件服务器
            smtp.Host </span>=<span style="color: #000000;"> mails.host;

            </span>//<span style="color: #000000;">邮件发送到SMTP服务器
            smtp.Send(mailMessage);
        }

        </span>/// &lt;summary&gt;
        ///<span style="color: #000000;"> 根据内容发送邮件（读取配置信息）
        </span>///<span style="color: #000000;"> 创建人：金永宝
        </span>///<span style="color: #000000;"> 创建时间：2020年8月11日
        </span>/// &lt;/summary&gt;
        /// &lt;<span style="color: #0000ff;">param</span> name=<span style="color: #800000;">"</span><span style="color: #800000;">mailTitle</span><span style="color: #800000;">"</span>&gt;&lt;/<span style="color: #0000ff;">param</span>&gt;
        /// &lt;<span style="color: #0000ff;">param</span> name=<span style="color: #800000;">"</span><span style="color: #800000;">mailBody</span><span style="color: #800000;">"</span>&gt;&lt;/<span style="color: #0000ff;">param</span>&gt;
        /// &lt;returns&gt;&lt;/returns&gt;<span style="color: #000000;">
        public static void SendMail(string mailTitle,string mailBody)
        {

            MailModel mails </span>=<span style="color: #000000;"> new MailModel();
            mails.fromPerson </span>= AppSettings.app(new string[] { <span style="color: #800000;">"</span><span style="color: #800000;">AppSettings</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">MailSetting</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">FromPerson</span><span style="color: #800000;">"</span><span style="color: #000000;"> });
            mails.code </span>= AppSettings.app(new string[] { <span style="color: #800000;">"</span><span style="color: #800000;">AppSettings</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">MailSetting</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">PassWord</span><span style="color: #800000;">"</span><span style="color: #000000;"> });
            mails.recipientArry </span>= AppSettings.app(new string[] { <span style="color: #800000;">"</span><span style="color: #800000;">AppSettings</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">MailSetting</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">RecipientArry</span><span style="color: #800000;">"</span><span style="color: #000000;"> }).Split(‘,‘);
            mails.mailCcArray </span>= AppSettings.app(new string[] { <span style="color: #800000;">"</span><span style="color: #800000;">AppSettings</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">MailSetting</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">MailCcArray</span><span style="color: #800000;">"</span><span style="color: #000000;"> }).Split(‘,‘);
            mails.mailBody </span>=<span style="color: #000000;"> mailBody;
            mails.mailTitle </span>=<span style="color: #000000;"> mailTitle;
            mails.host</span>= AppSettings.app(new string[] { <span style="color: #800000;">"</span><span style="color: #800000;">AppSettings</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">MailSetting</span><span style="color: #800000;">"</span>, <span style="color: #800000;">"</span><span style="color: #800000;">Host</span><span style="color: #800000;">"</span><span style="color: #000000;"> });

            string[] sArray </span>= mails.fromPerson.Split(new char[2] { ‘@<span style="color: #000000;">‘, ‘.‘ });
            </span><span style="color: #0000ff;">if</span> (sArray[1] == <span style="color: #800000;">"</span><span style="color: #800000;">qq</span><span style="color: #800000;">"</span><span style="color: #000000;">)
            {
                mails.host </span>= <span style="color: #800000;">"</span><span style="color: #800000;">smtp.qq.com</span><span style="color: #800000;">"</span>;//<span style="color: #000000;">如果是QQ邮箱则：smtp.qq.com,
            }
            </span><span style="color: #0000ff;">else</span> <span style="color: #0000ff;">if</span> (sArray[1] == <span style="color: #800000;">"</span><span style="color: #800000;">163</span><span style="color: #800000;">"</span><span style="color: #000000;">)
            {
                mails.host </span>= <span style="color: #800000;">"</span><span style="color: #800000;">smtp.163.com</span><span style="color: #800000;">"</span>;//如果是163邮箱则：smtp.163<span style="color: #000000;">.com
            }

            </span>//<span style="color: #000000;">将发件人邮箱带入MailAddress中初始化
            MailAddress mailAddress </span>=<span style="color: #000000;"> new MailAddress(mails.fromPerson);
            </span>//<span style="color: #000000;">创建Email的Message对象
            MailMessage mailMessage </span>=<span style="color: #000000;"> new MailMessage();

            </span>//<span style="color: #000000;">判断收件人数组中是否有数据
            </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (mails.recipientArry.Any())
            {
                </span>//<span style="color: #000000;">循环添加收件人地址
                </span><span style="color: #0000ff;">foreach</span> (var item <span style="color: #0000ff;">in</span><span style="color: #000000;"> mails.recipientArry)
                {
                    </span><span style="color: #0000ff;">if</span> (!<span style="color: #000000;">string.IsNullOrEmpty(item))
                        mailMessage.To.Add(item.ToString());
                }
            }

            </span>//<span style="color: #000000;">判断抄送地址数组是否有数据
            </span><span style="color: #0000ff;">if</span><span style="color: #000000;"> (mails.mailCcArray.Any())
            {
                </span>//<span style="color: #000000;">循环添加抄送地址
                </span><span style="color: #0000ff;">foreach</span> (var item <span style="color: #0000ff;">in</span><span style="color: #000000;"> mails.mailCcArray)
                {
                    </span><span style="color: #0000ff;">if</span> (!<span style="color: #000000;">string.IsNullOrEmpty(item))
                        mailMessage.To.Add(item.ToString());
                }
            }
            </span>//<span style="color: #000000;">发件人邮箱
            mailMessage.From </span>=<span style="color: #000000;"> mailAddress;
            </span>//<span style="color: #000000;">标题
            mailMessage.Subject </span>=<span style="color: #000000;"> mails.mailTitle;
            </span>//<span style="color: #000000;">编码
            mailMessage.SubjectEncoding </span>=<span style="color: #000000;"> Encoding.UTF8;
            </span>//<span style="color: #000000;">正文
            mailMessage.Body </span>=<span style="color: #000000;"> mails.mailBody;
            </span>//<span style="color: #000000;">正文编码
            mailMessage.BodyEncoding </span>= Encoding.<span style="color: #0000ff;">Default</span><span style="color: #000000;">;
            </span>//<span style="color: #000000;">邮件优先级
            mailMessage.Priority </span>=<span style="color: #000000;"> MailPriority.High;
            </span>//<span style="color: #000000;">正文是否是html格式
            mailMessage.IsBodyHtml </span>=<span style="color: #000000;"> mails.isbodyHtml;
            </span>//<span style="color: #000000;">取得Web根目录和内容根目录的物理路径
            string webRootPath </span>=<span style="color: #000000;"> string.Empty;

            </span>//<span style="color: #000000;">实例化一个Smtp客户端
            SmtpClient smtp </span>=<span style="color: #000000;"> new SmtpClient();
            </span>//<span style="color: #000000;">将发件人的邮件地址和客户端授权（局域网用密码）码带入以验证发件人身份
            smtp.Credentials </span>=<span style="color: #000000;"> new System.Net.NetworkCredential(mails.fromPerson, mails.code);
            </span>//<span style="color: #000000;">指定SMTP邮件服务器
            smtp.Host </span>=<span style="color: #000000;"> mails.host;

            </span>//<span style="color: #000000;">邮件发送到SMTP服务器
            smtp.Send(mailMessage);
        }
    }
}<br></span></pre>
 </div> <p>这是我的appsettings.json配置文件</p> <p><img alt="技术图片" src="http://image.tqcto.com/1597137643532.jpg?imageView2/0/q/75|watermark/2/text/54ix5LiK57yW56iLIHd3dy50cWN0by5jb20=/font/5a6L5L2T/fontsize/340/fill/IzAwMDAwMA==/dissolve/100/gravity/SouthEast/dx/10/dy/10" loading="lazy"></p>
 <div class="code">
  <pre><span style="color: #008000;">//</span><span style="color: #008000;">邮箱配置</span>
    <span style="color: #800000;">"</span><span style="color: #800000;">MailSetting</span><span style="color: #800000;">"</span><span style="color: #000000;">: {
      </span><span style="color: #800000;">"</span><span style="color: #800000;">Code</span><span style="color: #800000;">"</span>: <span style="color: #800000;">"</span><span style="color: #800000;">asdaasdasdas</span><span style="color: #800000;">"</span>, <span style="color: #008000;">//</span><span style="color: #008000;">授权码</span>
      <span style="color: #800000;">"</span><span style="color: #800000;">PassWord</span><span style="color: #800000;">"</span>: <span style="color: #800000;">"</span><span style="color: #800000;">123456</span><span style="color: #800000;">"</span><span style="color: #000000;">,  //发件人邮箱的密码
      </span><span style="color: #800000;">"</span><span style="color: #800000;">FromPerson</span><span style="color: #800000;">"</span>: <span style="color: #800000;">"</span><span style="color: #800000;"><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="1e7d7b6d76775e7d7d30707b6a">[email&nbsp;protected]</a></span><span style="color: #800000;">"</span>, <span style="color: #008000;">//</span><span style="color: #008000;">发件人</span>
      <span style="color: #800000;">"</span><span style="color: #800000;">RecipientArry</span><span style="color: #800000;">"</span>: <span style="color: #800000;">"</span><span style="color: #800000;"><a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="2142445249486142420f4f4455">[email&nbsp;protected]</a>,<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="325157415a5b007251511c5c5746">[email&nbsp;protected]</a></span><span style="color: #800000;">"</span>, <span style="color: #008000;">//</span><span style="color: #008000;">多个收件人用,号分割</span>
      <span style="color: #800000;">"</span><span style="color: #800000;">MailCcArray</span><span style="color: #800000;">"</span>: <span style="color: #800000;">"kaifalist</span><span style="color: #800000;">@cc.net</span><span style="color: #800000;">"</span>, <span style="color: #008000;">//</span><span style="color: #008000;">抄送 多个人用逗号分割</span>
      <span style="color: #800000;">"</span><span style="color: #800000;">IsbodyHtml</span><span style="color: #800000;">"</span>: <span style="color: #0000ff;">true</span>, <span style="color: #008000;">//</span><span style="color: #008000;">是否邮件体为Html</span>
      <span style="color: #800000;">"</span><span style="color: #800000;">Host</span><span style="color: #800000;">"</span>: <span style="color: #800000;">"</span><span style="color: #800000;">192.168.5.30</span><span style="color: #800000;">"</span> <span style="color: #008000;">//</span><span style="color: #008000;">smtp 服务器地址</span>
    }</pre>
 </div> <p>&nbsp;我用的outlook&nbsp; &nbsp;那个服务器信息 就是host ，密码在这里 也可以设置</p> <p>工具-账户设置-双击你的邮箱</p> <p><img alt="技术图片" src="http://image.tqcto.com/1597137644700.jpg?imageView2/0/q/75|watermark/2/text/54ix5LiK57yW56iLIHd3dy50cWN0by5jb20=/font/5a6L5L2T/fontsize/340/fill/IzAwMDAwMA==/dissolve/100/gravity/SouthEast/dx/10/dy/10" loading="lazy"></p> <p>最后就是调用直接传入标题和内容就可以了。亲测是可以的。我的版本是.net core 3.1</p> <p><img alt="技术图片" src="http://image.tqcto.com/1597137644988.jpg?imageView2/0/q/75|watermark/2/text/54ix5LiK57yW56iLIHd3dy50cWN0by5jb20=/font/5a6L5L2T/fontsize/340/fill/IzAwMDAwMA==/dissolve/100/gravity/SouthEast/dx/10/dy/10" loading="lazy"></p> <p>&nbsp;</p><p><a href="http://www.mamicode.com/info-detail-3075546.html" title=".net core 发送邮件,mamicode.com" style="color:#ffffff" target="_blank">.net core 发送邮件</a></p><p>原文地址：https://www.cnblogs.com/xiaojinFat/p/13475947.html</p></span>